package com.example.gokchinesefoodmapmcdev.controller;

import com.example.gokchinesefoodmapmcdev.entity.review.ReviewInfo;
import com.example.gokchinesefoodmapmcdev.service.ReviewService;
import com.example.gokchinesefoodmapmcdev.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/app/review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 新增评论
     * reviewInfo    包含从前端接收到的评分信息和订单id
     * @return
     */
    @PostMapping("/save")
    public ResultData addReviewInfo(@RequestBody Map<String, Object> map){
        ReviewInfo reviewInfo = new ReviewInfo();
        reviewInfo.setEffectiveNumber((Integer) map.get("effectiveNumber"));
        reviewInfo.setExpressNumber((Integer) map.get("expressNumber"));
        reviewInfo.setTasteNumber((Integer) map.get("tasteNumber"));
        reviewInfo.setWeightNumber((Integer) map.get("weightNumber"));
        Integer orderId = (Integer) map.get("orderNo");

        Integer result = reviewService.addReviewInfo(reviewInfo, orderId);

        if(result > 0){
            return ResultData.ok(ResultData.OPERATION_SUCCESS, "新增成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL, "新增失败");
    }

    /**
     * 通过商品ID获取评价列表
     *  goodId        商品id
     *  pageNumber    当前页码数
     *  pageSize      一页显示数量
     * @return
     */
    @PostMapping("/listByGoodId")
    public ResultData getReviewListByGoodId(@RequestBody Map<String, Integer> map){
        Integer goodId = map.get("goodId");
        Integer pageNumber = map.get("pageNumber");
        Integer pageSize = map.get("pageSize");
        List<Map<String, Object>> reviewInfoList = reviewService.getReviewInfoByGoodId(goodId, pageNumber, pageSize);
        if(reviewInfoList.isEmpty()){
            return ResultData.ok(ResultData.OPERATION_SUCCESS, "暂无评论信息");
        }
        return  new ResultData(ResultData.OPERATION_SUCCESS, "获取成功", reviewInfoList);
    }


}
